/*
 * ============================================================================
 *
 *       Filename:  apsync.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015年03月21日 14时06分21秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * ============================================================================
 */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include "config.h"
/* lua */
#include "aclua.h"
#include "common.h"

char *ap_sync(unsigned char *mac, char *status, int *needrestart)
{
	assert(luahandle.L_main != NULL);
	lua_State *L = luahandle.L_main;

	char macstr[MACSTR_LEN]; 
	mac_to_macstr(mac, macstr);

	aclua_getglobal(L, "ap_sync");
	lua_pushstring(L, macstr);
	lua_pushstring(L, status);
	aclua_pcall(L, 2, 2);

	if(!lua_isboolean(L,-2) ||
		!lua_isstring(L, -1)) {
		sys_err("<ap_sync> should return "
			"(bool,string)\n");
		exit(-1);
	}

	const char *tmp;
	char *cmd;
	*needrestart = lua_toboolean(L, -2);
	tmp = lua_tostring(L, -1);
	cmd = safe_malloc(strlen(tmp) + 1);
	strcpy(cmd, tmp);
	lua_pop(L, 2);

	return cmd;
}
